package com.taxshare.purchaser.service.rulesReview;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.taxshare.purchaser.constant.CodeConstant;
import com.taxshare.purchaser.entity.InvoiceCollectionEntity;
import com.taxshare.purchaser.mapper.InvoiceCollectionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 发票审核入口（更新发票审核状态）
 *
 * @author hjh
 */
@Component
public class RulesReview {

    @Autowired
    private InvoiceCollectionMapper collectionMapper;

    @Autowired
    private WhiteReviewCheck whiteReviewCheck;

    @Autowired
    private BlackReviewCheck blackReviewCheck;

    @Autowired
    private BuyerReviewCheck buyerReviewCheck;

    @Autowired
    private SellerReviewCheck sellerReviewCheck;

    @Autowired
    private DefaultReviewCheck defaultReviewCheck;

    @Autowired
    private GoodsReviewCheck goodsReviewCheck;

    @Autowired
    private AddressReviewCheck addressReviewCheck;
    /**
     * 校验发票规则
     *
     * @param bean 发票实体
     * @param id   审核规则id
     * @return
     */
    public InvoiceCollectionEntity checkInvoiceRules(InvoiceCollectionEntity bean, String id) {
        CheckInfoRespVO vo = new CheckInfoRespVO();
        CheckInfoRespVO checkInfoRespVO = new CheckInfoRespVO();
        List<RuleMainHandler> rules = new ArrayList<>();
        rules.add(whiteReviewCheck);
        rules.add(blackReviewCheck);
        rules.add(defaultReviewCheck);
        rules.add(sellerReviewCheck);
        rules.add(buyerReviewCheck);
        rules.add(goodsReviewCheck);
        rules.add(addressReviewCheck);
        StringBuilder notPassMsg = new StringBuilder("");
        StringBuilder notPassFlag = new StringBuilder(vo.getPass());
        for (RuleMainHandler rule : rules) {
            vo = rule.checkRule(bean, id);
            if (!vo.isGoing()) {
                notPassMsg.append(vo.getMsg()+";");
                notPassFlag.append(vo.getPass());
                checkInfoRespVO.setFlag(vo.isFlag());
            }
        }
        //审核通过更新发票审核信息
        if (checkInfoRespVO.isFlag()) {
            InvoiceCollectionEntity entity = new InvoiceCollectionEntity();
            UpdateWrapper<InvoiceCollectionEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("invoice_code", bean.getInvoiceCode())
                    .eq("invoice_no", bean.getInvoiceNo())
                    .set("rules_status", "2")
                    .set("rules_result", vo.getMsg())
                    .set("force_flag",notPassFlag.toString());
            bean.setRulesStatus("2");
            collectionMapper.update(entity, updateWrapper);
        }
        //审核不通过更新发票审核信息
        if (!checkInfoRespVO.isFlag()) {
            InvoiceCollectionEntity entity = new InvoiceCollectionEntity();
            UpdateWrapper<InvoiceCollectionEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("invoice_code", bean.getInvoiceCode())
                    .eq("invoice_no", bean.getInvoiceNo())
                    .set("rules_status", "3")
                    .set("rules_result", notPassMsg.toString())
                    .set("force_flag",notPassFlag.toString());
            bean.setRulesStatus("3");
            collectionMapper.update(entity, updateWrapper);
        }
        bean.setRulesResult(vo.getMsg());
        return bean;
    }

    /**
     * 未提交页面强制校验
     * @param bean
     * @return
     */
    public CheckInfoRespVO checkSubmit(InvoiceCollectionEntity bean,String uuid) {
        CheckInfoRespVO vo = new CheckInfoRespVO();
        String forceFlag = bean.getForceFlag();
        List<RuleMainHandler> rules = addList(forceFlag);
        for(RuleMainHandler rule : rules){
            vo = rule.checkSubmit(uuid);
            if(!vo.isGoing()){
                break;
            }
        }
        return vo;
    }

    /**
     * 根据规则标记添加规则
     * @param forceFlag
     * @return
     */
    private List<RuleMainHandler> addList(String forceFlag){
        List<RuleMainHandler> rules = new ArrayList<>();
        if(forceFlag.contains(CodeConstant.RULE_1)){
            rules.add(addressReviewCheck);
        }
        if(forceFlag.contains(CodeConstant.RULE_2)){
            rules.add(blackReviewCheck);
        }
        if(forceFlag.contains(CodeConstant.RULE_3)){
            rules.add(buyerReviewCheck);
        }
        if(forceFlag.contains(CodeConstant.RULE_4)){
            rules.add(defaultReviewCheck);
        }
        if(forceFlag.contains(CodeConstant.RULE_5)){
            rules.add(goodsReviewCheck);
        }
        if(forceFlag.contains(CodeConstant.RULE_6)){
            rules.add(sellerReviewCheck);
        }
        return rules;
    }
}
